const axios = require('axios')
const { query } = require('express')
const request = require('request')

const sectionList = require('./sectionList')
const {sectionListFour, allcountFour} = require('./sectionListfour')
console.log(sectionList.length)

// --------------------------------- 这部分是科一的变量和接口------------------------------
let myExamID = []
sectionList.forEach(function (item) {
  for (let i = item[0]; i <= item[1]; i++) {
    if (item.length == 3 && item[2] != 0) {
      myExamID.push(item[2].toString() + i)
    } else if (item.length == 3 && item[2] == 0) {
      myExamID.push(i.toString());
    }
  }
})

// 获取题目 科一
let Cqjson;
let allcount = myExamID.length;
function getModel(index) {
  // chushihua();
  
  console.log('allcount',allcount)
  if (index > myExamID.length)
    index = index % myExamID.length;
  let r = Math.random()
  let questionID = myExamID[index]
  let url = `https://mnks.jxedt.com/get_question?r=${r}&index=${questionID}`
  return url
 
}
// --------------------这部分是科四的接口，为了区分接口和变量的定义，
//科四统一在科一变量的基础上加上Four作为后缀

const myExamIDFour = [], myExamOrderFour = [], cIndexFour = 0, rightFour = 0, wrongFour = 0;
sectionListFour.forEach(function (item) {
  for (var i = item[0]; i <= item[1]; i++) {
    if (item.length == 3 && item[2] != 0) {
      myExamIDFour.push(item[2].toString() + i)
    } else if (item.length == 3 && item[2] == 0) {
      myExamIDFour.push(i.toString());
    }
  }
})
// 获取题目 科四 返回请求地址
function getModelFour(index) {
  // chushihua();
  //let allcount = myExamIDFour.length;
  if (index > myExamIDFour.length){
    index = index % myExamIDFour.length;
  }
  let BASEURI = "https://mnks.jxedt.com/get_question?";
  let method = "get";
  let parms = "r=" + Math.random() + "&index=" + myExamIDFour[index];
  return BASEURI + parms
}

function fetchPost(path, params) {
  return new Promise((resolve, reject) => {
    request.get(path, function (err, httpResponse, body) {
      if (err) {
        reject(err)
      } else {
        resolve(body)
      }
    })
  })
}


function API(url, data = {}, method = 'GET') {
  return new Promise((resolve, reject) => {
    let promise = null
    if (method === 'GET') {
      promise = axios({
        url,
        params: data
      })
    } else {
      promise = axios({
        url,
        data,
        method: 'POST'
      })
    }
    promise.then(res => {
      console.log('成功了')
      resolve(res.data)
    }).catch(err => {
      console.log('出错了')
      reject(err)
    })
  })
}
 
// --------------------------------驾考宝典接口 -------------------------------------------
// 支持的考试类型
let options = [{label:'科一',value:'/jiaokaoquestions', num: '/questionNum'},
{label:'科四', value:'/jkfour', num: '/questionNumFour'},]
module.exports = (router) => {
  // 请求题目 科一
  router.get('/jiaokaoquestions', async (req, res) => {
    const {index} = req.query
    if(index>=allcount){
      res.send({code:3,msg:'已经是最后一题'})
      return;
    }
    let url = getModel(index)
    let result = await API(url)
    
    console.log('这是result',typeof(result)) // obj
    //console.log(result)
    //res.json(result)
    let obj = {name:'f',age:20}
    res.send({code:0,data:result})
    
  })
  // 请求题目数量 科一
  router.get('/questionNum', async (req, res) => {
    res.send({code:0,data:allcount})
  })

  // 科四题目获取
  router.get('/jkfour', async (req, res) => {
    const {index} = req.query
    console.log('接口',index)
    if(index > allcountFour - 1){
      res.send({code:3,msg:'已经是最后一题'})
    }else if(index < 0){
      res.send({code:3,msg:'已经是第一题'})
    }else{
      let url = getModelFour(index)
      console.log(url)
      let result = await API(getModelFour(index))
      res.send({code:0,data:result})
    }
  })

  // 请求题目数量 科四
  router.get('/questionNumFour', async (req, res) => {
    res.send({code: 0, data: allcountFour})
  })

  // 返回支持的考试类型：返回格式 options:[{lable:'科一',value:'/jiaokaoquestions'},] val是后端请求地址
  router.get('/examtype', (req, res) =>{
    
    res.send({code: 200, data: options})
  })
}


// --------------------------------驾考宝典接口 结束-------------------------------------------